package demo.practice.dp;

public class P322 {

    public static void main(String[] args) {
        P322 p322 = new P322();
        p322.coinChange(new int[]{2}, 3);
    }

    public int coinChange(int[] coins, int amount) {
        int[] dp = new int[amount + 1];
        dp[0] = 0;

        //dp[i] 代表金额为i时，需要最少的零钱数
        //coins[j]代表当前零钱的面值

        //dp= Min( dp[i-coins[j]]+1)
        for (int i = 1; i < dp.length; i++) {
            dp[i] = Integer.MAX_VALUE;
            for (int j = 0; j < coins.length; j++) {
                if (i >= coins[j] && dp[i - coins[j]] <= amount) {
                    dp[i] = Math.min(dp[i - coins[j]] + 1, dp[i]);
                }
            }
        }
        return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
    }
}
